#include "ADD.h"
#include "Joueur.h"
using namespace std;

int main(int argc, char* argv[])
  /*
   * retourne 0 si exécution OK
   * retourne 1 si problème
   * retourne 2 si aide affichée
   */
{
  OutputLevel();

  dout(BLANK) << setfill('#') << setw(80) << '#' << endl << setfill(' ')
	      << '#' << setw(47) << "- NouveauJoueur -" << setw(32) << '#'
	      << endl << '#' << setw(79) << '#' << endl
	      << '#' << setw(56) << "Création d'un nouveau personnage"
	      << setw(24) << '#' << endl
	      << '#' << setw(44) << "Projet ADD" << setw(35) << '#' << endl
	      << '#' << setw(79) << '#' << endl
	      << '#' << setw(58) << "Développé par Johan \"Solo\" Luisier"
	      << setw(23) << '#' << endl
	      << '#' << setw(41) << "2007" << setw(38) << '#'
	      << endl << setfill('#') << setw(80) << '#' << endl << endreq;

  unsigned int i;
  string help("-h");

  if (argc == 1)
    {
      dout(ERR) << "Essayer ./NouveauJoueur -h" << endreq;
      return 1;
    }

  for(i = 0; i < argc; i++)
    {
      if (argv[i] == help)
	{
	  dout(BLANK) << "Utilisation ./NouveauJoueur Nom code_race code_classe"
		      << endl << "code_race :" << endl << setfill(' ')
		      << setw(6) << "1 =>" << setw(12) << "Humain"     << endl
		      << setw(6) << "2 =>" << setw(12) << "Nain"       << endl
		      << setw(6) << "3 =>" << setw(12) << "Elfe"       << endl
		      << setw(6) << "4 =>" << setw(12) << "Gnome"      << endl
		      << setw(6) << "5 =>" << setw(12) << "Hobbit"     << endl
		      << setw(6) << "6 =>" << setw(12) << "Demi-Elfe"  << endl
		      << setw(6) << "7 =>" << setw(12) << "Demi-orque" << endl
		      << setw(6) << "8 =>" << setw(12) << "Demi-Ogre"  << endl
		      << "code_classe :" << endl
		      << setw(6) << "1 =>" << setw(12) << "Guerrier"    << endl
		      << setw(6) << "2 =>" << setw(12) << "Paladin"     << endl
		      << setw(6) << "3 =>" << setw(13) << "Rôdeur"      << endl
		      << setw(6) << "4 =>" << setw(12) << "Voleur"      << endl
		      << setw(6) << "5 =>" << setw(12) << "Barde"       << endl
		      << setw(6) << "6 =>" << setw(13) << "Prêtre"      << endl
		      << setw(6) << "7 =>" << setw(12) << "Druide"      << endl
		      << setw(6) << "8 =>" << setw(12) << "Magicien"    << endl
		      << setw(6) << "9 =>" << setw(13) << "Spécialiste" << endreq;    
		      
	  return 0;
	}
    }
  vector<string> args;
  
  dout(DEBUG) << "Nbr arg av gérer_blancs " << argc << endreq;
  gerer_espaces_blancs(argv, argc, args);
  dout(DEBUG) << "Nbr arg ap gérer_blancs " << argc << endreq;

  for(unsigned int i(0); i < argc; i++)
    dout(DEBUG) << "Contrôle : args[" << i << "] = " << args[i] << endreq;    
  
  if (argc < 4)
    {
      dout(ERR) << "Utilisation  ./NouveauJoueur Nom code_race code_classe"
		<< endreq;
      dout(ERR) << "Essayer avec l'option -h pour l'aide" << endreq;
      return 1;
    }

  string traits[6] = {"Force", "Dextérité", "Constitution",
		      "Intelligence", "Sagesse", "Charisme"};
  string question;

  unsigned int x(0), tmp;
  unsigned int tab[7];

  dout(BLANK) << "Entrer vos traits bruts, les ajustements de races sont "
	      << "appliqués" << endl << "automatiquement après." << endreq;

  for(i = 0; i < 6; i++)
    {
      question = traits[i] + " : ";
      tmp = demander_unsigned_borne(question, 1, 19);
      tab[i] = tmp;
    }

  Race* gars;

  dout(DEBUG) << 1 << args[1] << " - " << args[2] << " - " << args[3] << endreq;

  string nom(args[1]);
  
  dout(DEBUG) << 2 << args[1] << " - " << args[2] << " - " << args[3] << endreq;

  string race;
 
  string truc(args[2]);

  dout(DEBUG) << 3 << args[1] << " - " << args[2] << " - " << args[3] << endreq;

  char quoi(truc[0]);
  switch (quoi)
    {
    case '1':
      gars = new Humain(x);
      race = "Humain";
      break;
    case '2':
      gars= new Nain(x);
      race = "Nain";
      break;
    case '3':
      gars = new Elfe(x);
      race = "Elfe";
      break;
    case '4':
      gars = new Gnome(x);
      race = "Gnome";
      break;
    case '5':
      gars = new Hobbit(x);
      race = "Hobbit";
      break;
    case '6':
      gars = new DemiElfe(x);
      race = "Demi-Elfe";
      break;
    case '7':
      gars = new DemiOrque(x);
      race = "Demi-Orque";
      break;
    case '8':
      gars = new DemiOgre(x);
      race = "Demi-Ogre";
      break;
    default:
      dout(ERR) << truc << endreq;
      dout(ERR) << "race inconnue" << endreq;
      exit(1);
  }

  gars -> appliquer_ajustements(tab);

  if (tab[0] == 18)
    {
      tmp = demander_unsigned_borne("Entrer le score de force exceptionnelle : ", 0, 100);
      tab[6] = tmp;
    }
  else
    tab[6] = 0;

  dout(DEBUG) << "Race OK" << endreq;

  vector<Classe*> war;
  Joueur *bob;

  for(i = 3; i < argc; i++)
    {
      truc = args[i];
      quoi = truc[0];
      switch(quoi)
	{
	case '1':
	  war.push_back(new Guerrier(1, tab, x));
	  break;
	case '2':
	  war.push_back(new Paladin(1, tab, x));
	  break;
	case '3':
	  war.push_back(new Rodeur(1, tab, x));
	  break;
	case '4':
	  war.push_back(new Voleur(1, tab, x));
	  break;
	case '5':
	  war.push_back(new Barde(1, tab, x));
	  break;
	case '6':
	  war.push_back(new Clerc(1, tab, x));
	  break;
	case '7':
	  war.push_back(new Druide(1, tab, x));
	  break;
	case '8':
	  war.push_back(new Mage(1, tab, x));
	  break;
	case '9':
	  war.push_back(new Spec(1, tab, x));
	  break;
	default:
	  dout(ERR) << "Classe inconnue" << endreq;
	  return 1;
	}
      
      dout(DEBUG) << "Classe OK" << endreq;

      war.back() -> appliquer_modif(race);

      if (i == 3)
	bob = new Joueur(war.back(), gars, nom);
      else
	bob -> ajout_classe(war.back());
    }

  tmp = demander_unsigned("Entrer le nombre de PV : ");

  bob -> set_pv(tmp);

  dout(DEBUG) << "Joueur OK" << endreq;

  dout(FUNC) << "PPs restants : " << x << endreq;

  bob -> set_pp(x);
  
  //bob -> info();

  bob -> determiner_dons_desavantages(Desav);
  bob -> determiner_dons_desavantages(Don);  
  bob -> determiner_competences();
  bob -> augmenter_competences();

  bob -> ecrire_xml();

  dout(INFO) << war.back() -> get_competence(0) << endreq;

  delete gars;
  do
    {
      delete war.back();
      war.pop_back();
    }
  while(!war.empty());
  delete bob;
  
  return 0;
}
